# -*- coding: utf-8 -*-
"""
@time：  2021/04/28  22:31
@Auto：Mr. xingye  993843278
@Company 星野训练营 99384378@qq.com
@Function:qq的滑块验证码

"""
import time

from requests import get
from selenium import webdriver
from selenium.webdriver import ActionChains


from common.ver.slide import Slide

driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(3)
driver.get('https://passport.juneyaoair.com/?backUrl=http://b2c.preview.juneyaoair.com/index.aspx')
# driver.switch_to.frame('login_frame')
driver.find_element_by_xpath('//*[@id="accountName"]').send_keys('13893233624')
driver.find_element_by_xpath('//*[@id="pwd"]').send_keys('jx123456')
driver.find_element_by_xpath('//*[@id="checkbox-account"]').click()
driver.find_element_by_xpath('//*[@id="accountLoginBtn"]').click()

# driver.switch_to.frame('//html/body/iframe')
driver.switch_to.frame(driver.find_element_by_xpath('//html/body/iframe'))

while True:
    #获取下载链接
    ele_bg = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div[6]/div/div[1]/div[1]/div/a/div[1]/div/canvas[2]')
    src_bg = ele_bg.get_attribute('src')
    # print(src_bg)

    ele_Block = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div[6]/div/div[1]/div[1]/div/a/div[1]/div/canvas[2]')
    src_Block = ele_Block.get_attribute('src')
    # print(src_Block)

    #下载图片
    # 得到图片的二进制码，浏览器看到的图片解析了二进制码
    bg_con = get(src_bg).content
    print("bg_con:",bg_con)
    #打开图片，没有保存图片
    f = open('../lib/verify/bg.jpg',mode='wb')
    # 写入图片的二进制码
    f.write(bg_con)
    f.close()

    Block_con = get(src_Block).content
    #打开图片，没有保存图片
    f = open('../lib/verify/Block.jpg',mode='wb')
    f.write(Block_con)
    f.close()



    slide = Slide(driver)

    s = ele_bg.size
    print(s)
    x = slide.FindPic("../lib/verify/bg.jpg","../lib/verify/Block.jpg")
    x = int(x * ele_bg.size.get('width') /680)
    print(x)

    action = ActionChains(driver)
    # 按住
    action.click_and_hold(ele_Block).perform()
    #拖动
    action.move_by_offset(x,0).perform()
    #放开鼠标
    action.release().perform()
    time.sleep(2)
    #判断否有刷新按钮来确定是否重试
    try:
        driver.find_element_by_xpath('//*[@id="e_reload"]').click()
    except:
        break
driver.quit()